package com.zhiyou.service.ContractServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.bean.Contract;
import com.zhiyou.dao.ContractMapper;
import com.zhiyou.service.ContractService.ContractServiec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class ContractServiecImpl implements ContractServiec {

    @Autowired
    ContractMapper contractMapper;

    @Override
    public PageInfo getContractMsg(Integer pn,Contract contract) {
        PageHelper.startPage(pn,20);
        List<Contract> contractMsg = contractMapper.getLikeContract(contract);
        PageInfo page = new PageInfo(contractMsg);
        return page;
    }

    @Override
    public Contract getContractDetailMsg(Contract contract) {
        return contractMapper.getContractMsg(contract);
    }

    @Override
    public int deleteContract(Integer cid) {
        Example ex = new Example(Contract.class);
        Example.Criteria criteria = ex.createCriteria();
        criteria.andEqualTo("cId",String.valueOf(cid));
        Contract con = new Contract();
        con.setcStatus(0);
        return contractMapper.updateByExampleSelective(con,ex);
    }

    @Override
    public int updateContract(Contract contract,String overtime) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-mm-dd");
        try {
            contract.setcOvertime(sd.parse(overtime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return contractMapper.updateByPrimaryKeySelective(contract);
    }

    @Override
    public int addContract(Contract contract, String onTime, String startTime, String overTime) {

        Integer i = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
            contract.setcTime(sdf.parse(onTime));
            contract.setcTaketime(sdf.parse(startTime));
            contract.setcOvertime(sdf.parse(overTime));
            i = contractMapper.insertSelective(contract);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public List<Contract> exportExcel() {
        return contractMapper.getMonth();
    }


}
